//
//  GVIndexMessageCell.m
//  GmallVIP
//
//  Created by yemeihua on 15/9/10.
//  Copyright (c) 2015年 yemeihua. All rights reserved.
//

#import "GVIndexMessageCell.h"

//@interface GMTrack : NSObject<DOUAudioFile>
//
//@property (nonatomic,copy) NSURL *audioFileURL;
//
//@end
//
//@implementation GMTrack
//
//@end

@interface GVIndexMessageCell()<TTTAttributedLabelDelegate>

//@property (nonatomic,readonly) DOUAudioStreamer *streamer;
@property (nonatomic,readonly) NSTimer *timer;

@end

@implementation GVIndexMessageCell

@synthesize  updatedConstraints = _updatedConstraints;

+ (NSString *)identifier
{
    return NSStringFromClass(self);
}

+ (UIFont *)messageContentFont
{
    static UIFont *font;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        font = [UIFont fontWithName:@"DFHanziPenW3-GB" size:18];
        if(!font)
        {
            font = [UIFont systemFontOfSize:18];
        }
    });
    return font;
}

+ (UIFont *)timeFont
{
    static UIFont *font;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        font = [UIFont systemFontOfSize:12];
    });
    return font;
}

+ (UIFont *)nameFont
{
    static UIFont *font;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        font = [UIFont systemFontOfSize:13];
    });
    return font;
}

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        _avatorContainerView = [[UIImageView alloc] init];
        _avatorContainerView.image = [UIImage imageNamed:@"avator_shadow"];
        _avatorContainerView.userInteractionEnabled = YES;
        _avatorContainerView.opaque = YES;
        [self.contentView addSubview:_avatorContainerView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchAvatorImageViewFromTapGestureRecognizer:)];
        [_avatorContainerView addGestureRecognizer:tap];
        
        _avatorImageView = [[UIImageView alloc] init];
        _avatorImageView.contentMode = UIViewContentModeScaleAspectFill;
        [_avatorContainerView addSubview:_avatorImageView];
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = [UIColor yc_colorWithUInt:0xa1a1a1];
        _timeLabel.font = [[self class] timeFont];
        _timeLabel.opaque = YES;
        [self.contentView addSubview:_timeLabel];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = [[self class] nameFont];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.opaque = YES;
        [self.contentView addSubview:_nameLabel];
        
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.opaque = YES;
        [self.contentView addSubview:_mainView];
        
        _messageLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)];
        _messageLabel.numberOfLines = 0;
        _messageLabel.userInteractionEnabled = YES;
        _messageLabel.font = [[self class] messageContentFont];
        _messageLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _messageLabel.textAlignment = NSTextAlignmentLeft;
        _messageLabel.opaque = YES;
        _messageLabel.hidden = YES;
        _messageLabel.backgroundColor = [UIColor whiteColor];
        _messageLabel.delegate = self;
        _messageLabel.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:[UIColor yc_colorWithUInt:0x007eff]};
        _messageLabel.extendsLinkTouchArea = YES;
        _messageLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
        _messageLabel.preferredMaxLayoutWidth = kScreenWidth - 108;
        [self.mainView addSubview:_messageLabel];
        
        _messageImageView = [[GVMessageImageView alloc] init];
        _messageImageView.contentMode = UIViewContentModeScaleAspectFill;
        _messageImageView.layer.masksToBounds = YES;
        _messageImageView.userInteractionEnabled = YES;
        _messageImageView.opaque = YES;
        _messageImageView.hidden = YES;

        [self.mainView addSubview:_messageImageView];
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessageImageViewFromTapGestureRecognizer:)];
        [_messageImageView addGestureRecognizer:tap];
        
        _messageVideoView = [[GVMessageVideoView alloc] init];
        _messageVideoView.contentMode = UIViewContentModeScaleAspectFill;
        _messageVideoView.layer.masksToBounds = YES;
        _messageVideoView.opaque = YES;
        _messageVideoView.hidden = YES;
        [self.mainView addSubview:_messageVideoView];
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessageVideoViewFromTapGestureRecognizer:)];
        [_messageVideoView addGestureRecognizer:tap];
        
        _messageVoiceView = [[GVMessageVoiceView alloc] init];
        _messageVoiceView.layer.masksToBounds = YES;
        _messageVoiceView.opaque = YES;
        _messageVoiceView.hidden = YES;
        [self.mainView addSubview:_messageVoiceView];
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessageVoiceViewFromTapGestureRecognizer:)];
        [_messageVoiceView addGestureRecognizer:tap];
        
        _messageProductView = [[GVMessageProductView alloc] init];
        _messageProductView.opaque = YES;
        _messageProductView.hidden = YES;
        [_messageProductView.seeButton addTarget:self action:@selector(didTouchUpInsideProductViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [_messageProductView.buyButton addTarget:self action:@selector(didTouchUpInsideProductViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainView addSubview:_messageProductView];
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessageProductViewFromTapGestureRecognizer:)];
        [_messageProductView addGestureRecognizer:tap];
        
        {
            _messagePushView = [[GVMessagePushView alloc] init];
            _messagePushView.opaque = YES;
            _messagePushView.hidden = YES;
            
            [self.mainView addSubview:_messagePushView];
            
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessagePushViewFromTapGestureRecognizer:)];
            [_messagePushView addGestureRecognizer:tap];
        }
        
        {
            _messagePushView0 = [[GVMessagePushView0 alloc] init];
            _messagePushView0.opaque = YES;
            _messagePushView0.hidden = YES;
            
            [self.mainView addSubview:_messagePushView0];
            
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessagePushViewFromTapGestureRecognizer:)];
            [_messagePushView0 addGestureRecognizer:tap];
        }
        
        {
            _messagePushView1 = [[GVMessagePushView1 alloc] init];
            _messagePushView1.opaque = YES;
            _messagePushView1.hidden = YES;
            
            [self.mainView addSubview:_messagePushView1];
            
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessagePushViewFromTapGestureRecognizer:)];
            [_messagePushView1 addGestureRecognizer:tap];
        }
        
        {
            _messagePushView2 = [[GVMessagePushView2 alloc] init];
            _messagePushView2.opaque = YES;
            _messagePushView2.hidden = YES;
            
            [self.mainView addSubview:_messagePushView2];
            
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessagePushViewFromTapGestureRecognizer:)];
            [_messagePushView2 addGestureRecognizer:tap];
        }
        
        {
            _messagePushView3 = [[GVMessagePushView3 alloc] init];
            _messagePushView3.opaque = YES;
            _messagePushView3.hidden = YES;
            
            [self.mainView addSubview:_messagePushView3];
            
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessagePushViewFromTapGestureRecognizer:)];
            [_messagePushView3 addGestureRecognizer:tap];
        }
        
        _messageRegisterView = [[GVMessageRegisterView alloc] init];
        _messageRegisterView.opaque = YES;
        _messageRegisterView.hidden = YES;
        [_messageRegisterView.seeButton addTarget:self action:@selector(didTouchUpInsideRegisterViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [_messageRegisterView.registerButton addTarget:self action:@selector(didTouchUpInsideRegisterViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainView addSubview:_messageRegisterView];
        
        _messageNoticeView = [[GVMessageOrderNoticeView alloc] init];
        _messageNoticeView.opaque = YES;
        _messageNoticeView.hidden = YES;
        [_messageNoticeView.goButton addTarget:self action:@selector(didTouchUpInsideOrderNoticeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainView addSubview:_messageNoticeView];
        
        _messageMusicView = [[GVMessageMusicView alloc] init];
        _messageMusicView.opaque = YES;
        _messageMusicView.hidden = YES;
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchMessageMusicViewFromTapGestureRecognizer:)];
        [_messageMusicView.imageView addGestureRecognizer:tap];
        
        [self.mainView addSubview:_messageMusicView];
        
        _gifImageView = [[UIImageView alloc] init];
        _gifImageView.contentMode = UIViewContentModeScaleAspectFill;
        _gifImageView.layer.masksToBounds = YES;
        _gifImageView.userInteractionEnabled = YES;
        _gifImageView.opaque = YES;
        _gifImageView.hidden = YES;
        
        [self.mainView addSubview:_gifImageView];
    }
    return self;
}

- (void)didTouchAvatorImageViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideAvatorImageView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideAvatorImageView:self.avatorImageView];
    }
}

- (void)didTouchMessageImageViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageImageView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageImageView:self.messageImageView];
    }
}

- (void)didTouchMessageProductViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageProductView:button:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageProductView:self.messageProductView button:nil];
    }
}

- (void)didTouchMessageVoiceViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageVoiceView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageVoiceView:self.messageVoiceView];
    }
}

- (void)didTouchMessageVideoViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageVideoView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageVideoView:self.messageVideoView];
    }
}

- (void)didTouchMessagePushViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageVoiceView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessagePushView:(GVMessagePushView *)recognizer.view];
    }
}

- (void)didTouchUpInsideProductViewButton:(UIButton *)button
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageProductView:button:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageProductView:self.messageProductView button:button];
    }
}

- (void)didTouchUpInsideRegisterViewButton:(UIButton *)button
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageRegisterView:button:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageRegisterView:self.messageRegisterView button:button];
    }
}

- (void)didTouchUpInsideOrderNoticeButton:(UIButton *)button
{
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageOrderNoticeView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageOrderNoticeView:self.messageNoticeView];
    }
}

- (void)didTouchMessageMusicViewFromTapGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
//    if(self.streamer)
//    {
//        [self restore];
//    }
//    else
//    {
//
//        [[NSNotificationCenter defaultCenter] postNotificationName:GMPlayMusicNotification object:nil];
//        
//        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_timerAction:) userInfo:nil repeats:YES];
//        
//        NSString *url = [self.message.ext[GVMessageExtInfoMusicURLKey] urlEncodeString];
//        GMTrack *track = [[GMTrack alloc] init];
//        track.audioFileURL = [NSURL URLWithString:url];
//        _streamer = [[DOUAudioStreamer alloc] initWithAudioFile:track];
//        
//        GM_WEAK;
//        
//        [self.KVOController observe:_streamer keyPath:@"status" options:NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
//            [weakSelf performSelector:@selector(_updateStatus)
//                         onThread:[NSThread mainThread]
//                       withObject:nil
//                    waitUntilDone:NO];
//        }];
//        
//        [self.KVOController observe:_streamer keyPath:@"duration" options:NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
//            [weakSelf performSelector:@selector(_timerAction:)
//                         onThread:[NSThread mainThread]
//                       withObject:nil
//                    waitUntilDone:NO];    }];
//        
//        [self.KVOController observe:_streamer keyPath:@"bufferingRatio" options:NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
//            [weakSelf performSelector:@selector(_updateStatus)
//                         onThread:[NSThread mainThread]
//                       withObject:nil
//                    waitUntilDone:NO];
//        }];
//        
//        [_streamer play];
//        
//        [self _updateStatus];
//        [self _updateBufferingStatus];
//        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRecievePlayMusicNotification:) name:GMPlayMusicNotification object:nil];
    //}
    
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideMessageMusicView:)])
    {
        [self.delegate didMessageCell:self touchUpInsideMessageMusicView:self.messageMusicView];
    }
}

//#pragma mark -- 音乐处理
//
//- (void)didRecievePlayMusicNotification:(NSNotification *)notification
//{
//    [self restore];
//}
//
//- (void)restore
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:GMPlayMusicNotification object:nil];
//    [self.KVOController unobserve:_streamer];
//    
//    [_timer invalidate];
//    _timer = nil;
//    
//    [self.streamer stop];
//    _streamer = nil;
//    
//    self.messageMusicView.progressView.progress = 0;
//    self.messageMusicView.stateImageView.image = [UIImage imageNamed:@"msg_music_play"];
//}
//
//- (void)_updateStatus
//{
//    switch ([self.streamer status])
//    {
//        case DOUAudioStreamerPlaying:
//            self.messageMusicView.stateImageView.image = [UIImage imageNamed:@"msg_music_stop"];
//            if([self.messageMusicView.indicatorView isAnimating])
//            {
//                [self.messageMusicView.indicatorView stopAnimating];
//            }
//            break;
//            
//        case DOUAudioStreamerPaused:
//            break;
//            
//        case DOUAudioStreamerIdle:
//            break;
//            
//        case DOUAudioStreamerFinished:
//            [self restore];
//            break;
//            
//        case DOUAudioStreamerBuffering:
//            if(!self.messageMusicView.indicatorView.isAnimating)
//            {
//                self.messageMusicView.stateImageView.image = [UIImage imageNamed:@"msg_music_load"];
//                [self.messageMusicView.indicatorView startAnimating];
//            }
//            break;
//            
//        case DOUAudioStreamerError:
//            [self restore];
//            break;
//    }
//}
//
//- (void)_updateBufferingStatus
//{
//    NSString *statusString = [NSString stringWithFormat:@"Received %.2f/%.2f MB (%.2f %%), Speed %.2f MB/s", (double)[self.streamer receivedLength] / 1024 / 1024, (double)[self.streamer expectedLength] / 1024 / 1024, [self.streamer bufferingRatio] * 100.0, (double)[self.streamer downloadSpeed] / 1024 / 1024];
//    DDLogVerbose(@"%@",statusString);
//    
//    if ([self.streamer bufferingRatio] >= 1.0) {
//        DDLogVerbose(@"sha256: %@", [self.streamer sha256]);
//    }
//}
//
//- (void)_timerAction:(id)timer
//{
//    if ([self.streamer duration] == 0.0)
//    {
//        self.messageMusicView.progressView.progress = 0.0f;
//    }
//    else
//    {
//        self.messageMusicView.progressView.progress = [self.streamer currentTime] / [self.streamer duration];
//    }
//}

#pragma mark -- TTTAttributedLabel delegate

- (void)attributedLabel:(__unused TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    YCLog(@"%@",url.description);
    if([self.delegate respondsToSelector:@selector(didMessageCell:touchUpInsideLinkURL:)])
    {
        [self.delegate didMessageCell:self touchUpInsideLinkURL:url];
    }
}

@end
